package com.ruoyi;

import com.ruoyi.common.utils.security.Md5Utils;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;

public class LicenseUtils {

    public static boolean checkLicense(String licensePath) throws Exception{
        String license = readFile(licensePath);
        String macStr = getMacList().toString();
        return license.equals(buildLicense(macStr));
    }

    public static List<String> getMacList() throws Exception {
        java.util.Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        StringBuilder sb = new StringBuilder();
        ArrayList<String> tmpMacList=new ArrayList<>();
        while(en.hasMoreElements()){
            NetworkInterface iface = en.nextElement();
            List<InterfaceAddress> addrs = iface.getInterfaceAddresses();
            for(InterfaceAddress addr : addrs) {
                InetAddress ip = addr.getAddress();
                NetworkInterface network = NetworkInterface.getByInetAddress(ip);
                if(network==null){continue;}
                byte[] mac = network.getHardwareAddress();
                if(mac==null){continue;}
                sb.delete( 0, sb.length() );
                for (int i = 0; i < mac.length; i++) {sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));}
                tmpMacList.add(sb.toString());
            }        }
        if(tmpMacList.size()<=0){return tmpMacList;}
        List<String> unique = tmpMacList.stream().distinct().collect(Collectors.toList());
        return unique;
    }

    private static String readFile(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        byte[] bytes = Files.readAllBytes(path);
        return new String(bytes);
    }

    private static String buildLicense(String macStr) throws Exception {
        String linceseStr = Md5Utils.hash(macStr);
        linceseStr += "ZMOcxAmSjmDfP2WzeUakDEcSg33whBPoCt1LvoXgOgpd8RyzEAQZXTFUotpEefbZelXqFUv6Oo1gJJc4jy2ioL9U36sBKOtBY7jSgIETJMMsCS82FW7eJpyaKQqjJ0k5D5kM4mKTrdKiX0RaRis4nRjyH9IIICJx7DtPbaVXrkllZJ0QUBhxSTPqjyHhkZW5KoUPRhzFAoEe1QTBfwbJ1hZq2L89WbYlwdjxRLS4r5QafaNWfdpeA7K0bKZ4Xmg7dBh5Swpo8pQrhjvxreocsZz5Ttl9F5blJer8MLIxyPNEZOv636aFdtDdpskKHindodJR5xBj9N64nlGwd1lJGKAtWNs4Qsyh8BcCQkuQSADUD2Fqouis9DlU5XpUucgWoLjtsQGba0bC9uJ1DUTmL4TJWhgvAVfVukvxjeO9byB4soV2btqozsJD8PTXWHTUm2yo3znz9vsJHcGIQCLSx33gckemTJ3deOSA7N2WAvfrIPpqW4VZyMf8gxM2WJgPhW4b2J99KgKf3dVOohkdCLZGlNvIYjQIrowXNti1xA5ZMjZRH6yWGN8dHj0XJCygHXl56VCnciXlHkrQPEjqdrUCm25Lbg0PrndhS9HQ1KjpCiquFFsOXlKLWbG9gLyl9kQLrjydBhyEwWtruBNRPfOI0I360DXPlPaQePcKLDA4nCRjXqd8ONDW8z97wQi0NczwVnIC4e7lcxIJ26GLdBmRrOTo4EMPXJ3tPcJQNMCyhVTocyPODSRzWPjpSYIW011GO41BZJspZkeRpvRDrMWxOZ9HPuvEg4hdBhPxtIg6797EQ7odB2PI6H4QYGBYq85Aqo7eTp0eQfUSPfFzsODocKA2ztNn1jPThJ1xrvvCIDPowNDnTRP8xteByWQHqI5rVc9711VO33WY2xgNas1YxzjWLHNYXfeVLMLkCxGFhwpbNFOAtJ2KLRAiyLCK9Cub7o6AoXl8urk38GJQskatXtaSa4ki3q4zB44Z";
        byte[] bytes = linceseStr.getBytes();
        return Base64.getEncoder().encodeToString(bytes);
    }


    public static void main(String[] args) throws Exception {
        System.out.println(buildLicense("macStr"));
    }
}
